package com.designPattern.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Auther: zwg
 * @Date: 2019/2/20 20:04
 * @Description:
 */
public class ProxyFactory implements InvocationHandler {
    private Class<?> target;
    private Object real;

    public ProxyFactory(Class<?> target) {
        this.target = target;
    }

    public Object bind(Object real){
        this.real = real;
        return Proxy.newProxyInstance(this.target.getClassLoader(), new Class[]{target}, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");
        Object invoke = method.invoke(real, args);
        System.out.println("after");
        return invoke;
    }


}
